package com.hnhegui.backend.platform.service.merchant;

import com.hnhegui.backend.entity.PageResult;
import com.hnhegui.backend.entity.Result;
import com.hnhegui.backend.merchant.api.ProductRestApi;
import com.hnhegui.backend.merchant.dto.request.ProductPageRequestDTO;
import com.hnhegui.backend.merchant.dto.response.ProductDetailsResponseDTO;
import com.hnhegui.backend.platform.convert.merchant.ProductConvert;
import com.hnhegui.backend.platform.vo.merchant.common.ProductListItem;
import com.hnhegui.backend.platform.vo.merchant.response.ProductDetailResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RestProductService {

    @Autowired
    private ProductRestApi productRestApi;

    public PageResult<ProductListItem> getProductList(String category, String keyword, String sortBy, String sortOrder, Integer page, Integer pageSize) {
        ProductPageRequestDTO request = new ProductPageRequestDTO();
        request.setCategory(category);
        request.setKeyword(keyword);
        request.setSortBy(sortBy);
        request.setSortOrder(sortOrder);
        if (page != null) {
            request.setPage(page);
        }
        if (pageSize != null) {
            request.setPageSize(pageSize);
        }
        Result<PageResult<ProductDetailsResponseDTO>> productList = productRestApi.getProductList(request);
        return ProductConvert.INSTANCE.convertPage(productList.getData());
    }

    public ProductDetailResponse getProductDetail(Long productId) {
        Result<ProductDetailsResponseDTO> productDetail = productRestApi.getProductDetail(productId);
        return ProductConvert.INSTANCE.convert(productDetail.getData());
    }
}
